﻿
//购物车页面验证处理
$(document).ready(function () {
    $('#submit_bt').click(function () {
        if (confirm("是否确认进入付款页面\n立即付款？") == true) {
            if ($('#uname').val() == "") {
                alert("真实姓名不能为空！");
                $('#uname').focus();
                return false;

            }
            else {
                if ($('#uname').val().length > 20) {
                    alert("真实姓名太长了,请重新输入！");
                    $('#uname').focus();
                    return false;
                }

            }


            if ($('#tel').val() == "") {

                alert("联系电话不能为空！");
                $('#tel').focus();
                return false;
            }

            if ($('#tel').val() != "") {

                var str = $('#tel').val();

                var mobile = /^(13[0-9]|15[0-9]|18[0-9])\d{8}$/;
                var tel = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;

                str = jQuery.trim(str);

                if (!mobile.test(str)) {
                    if (!tel.test(str)) {

                        alert("联系电话的输入格式不正确！");
                        $('#tel').focus();
                        return false;
                    }
                }
                if (!tel.test(str)) {
                    if (!mobile.test(str)) {

                        alert("联系电话的输入格式不正确！");
                        $('#tel').focus();
                        return false;
                    }
                }

            }



            for (var i = 0; i < $("input[name=counts]").length; i++) {

                if (!isNaN($($("input[name=counts]")[i]).val())) {

                    $($("ck_count")[i]).text("");
                    if ($($("input[name=counts]")[i]).val() == "") {

                        $($("ck_count")[i]).text("*数量不能为空");
                        $($("input[name=counts]")[i]).focus();
                        return false;
                    }
                    else {
                        if ($($("input[name=counts]")[i]).val() < 1) {
                            $($("ck_count")[i]).text("*数量不能小于1");
                            $($("input[name=counts]")[i]).focus();
                            return false;
                        }
                        $($("ck_count")[i]).text("");


                    }

                }
                else {
                    $($("ck_count")[i]).text("*数量输入非法");
                    $(this).focus();
                    return false;

                }

            }
            document.getElementById("form1").action = "shopping22.aspx";
            $('#form1').submit();
            //  return false;
        }



    });

    //文本框失去焦点时验证数量值是否发生变化
    $("input[name=counts]").blur(function () {
        var count = $(this).val();
        var allsum = 0;
        var pid = $(this).attr("pid");
        if (!isNaN(count)) {
            $('#ck_count' + pid).text("");
            if (count == "") {

                $('#ck_count' + pid).text("*数量不能为空");
                $(this).focus();
                return false;
            }
            else {
                if (count < 1) {
                    $('#ck_count' + pid).text("*数量不能小于1");

                    $(this).focus();
                    return false;
                }

                $('#ck_count' + pid).text("")
                var price = parseFloat($(this).attr("price"));
                var sum = parseInt(count) * price;

                $.post("ChangeCount.ashx", { pid: pid, count: count }, function (data) {

                    $('#amount' + pid).text(sum + " 元");
                    allsum = data;

                    $('#sums').text("合计：" + allsum + " 元");


                })

            }
        }
        else {
            $('#ck_count' + pid).text("*输入非法!");
            $(this).focus();
            return false;
        }
    });



});


//产品数量增加
function xun_zeng(id) {
    var counts = $("[name=counts]");
    var allsum = 0;
    var sum = 0;
    var result = null;
    for (var i = 0; i < counts.length; i++) {
        if ($(counts[i]).attr("pid") == id) {
            var num = $(counts[i]).val();
            if (num >= 1)
                num++;

            else
                num = 1;

            $(counts[i]).val(num);
        }
        var pid = $(counts[i]).attr("pid");
        var count = $(counts[i]).val();
        var sum = count * parseFloat($(counts[i]).attr("price"));
        $('#amount' + pid).text(sum + " 元");
        $.post("ChangeCount.ashx", { pid: pid, count: count }, function (data) {
            if (data != "") {

                allsum = data;
                $('#sums').text("合计：" + allsum + " 元");
            }
        })
    }

}
//产品数量减少
function xun_jian(id) {
    var counts = $("[name=counts]");
    for (var i = 0; i < counts.length; i++) {
        if ($(counts[i]).attr("pid") == id) {
            var num = $(counts[i]).val();
            if (num <= 1)
                num = 1;
            else
                num--;
            $(counts[i]).val(num);
        }


        var pid = $(counts[i]).attr("pid");
        var count = $(counts[i]).val();
        var sum = count * parseFloat($(counts[i]).attr("price"));
        $('#amount' + pid).text(sum + " 元");
        $.post("ChangeCount.ashx", { pid: pid, count: count }, function (data) {
            if (data != "") {

                allsum = data;
                $('#sums').text("合计：" + allsum + " 元");
            }
        })

    }

}